using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SyncUdp
{
    using SyncUdp.Interface;
    using System;

    /// <summary>
    /// 抽象单例类
    /// </summary>
    public abstract class AbstractSingleton : ISingleton
    {
        public ISingleton Instance => this;

        public bool IsInited { get; private set; }

        public void Init()
        {
            if (IsInited) 
                return;
            OnInit();
            IsInited = true;
        }

        protected abstract void OnInit();

        protected void RegisterEvent<T>(Action<T> action) where T : IEventArgs
        {
            var eventCenter = IOCContainer.GetInstance<IEventCenter>();
            eventCenter.RegisterEvent<T>(action);
        }

        protected void UnregisterEvent<T>() where T : IEventArgs
        {
            var eventCenter = IOCContainer.GetInstance<IEventCenter>();
            eventCenter.UnregisterEvent<T>();
        }

        protected void SendEvent<T>(T eventArgs) where T : IEventArgs
        {
            var eventCenter = IOCContainer.GetInstance<IEventCenter>();
            eventCenter.SendEvent(eventArgs);
        }
    }
}
